﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading;

namespace Wormhole.UI.Application.Controls.Cards
{
	/// <summary>
	/// Interaction logic for CardPiles.xaml
	/// </summary>
	public partial class CardPiles : UserControl
	{
		#region Player Score
		private Engine.Player.Player _player;
		/// <summary>
		/// The game engine object holding the player score.
		/// </summary>
		public Engine.Player.Player Player
		{
			get
			{
				return this._player;
			}

			set
			{
				this._player = value;
				this.SetEngineEvents();
				this.DisplayPlayerInformation();
			}
		}
		#endregion

		#region Constructor
		/// <summary>
		/// Initialize a default instance of the class.
		/// </summary>
		public CardPiles()
		{
			InitializeComponent();
		}
		#endregion

		#region Set Engine Events
		/// <summary>
		/// Subscribe to events from the engine objects.
		/// </summary>
		protected void SetEngineEvents()
		{
			// subscribe to deck events
			this.Player.Deck.CardTaken += new Wormhole.Engine.Decks.Deck.CardTakenDelegate(Deck_CardTaken);
			this.Player.Deck.CardAdded += new Wormhole.Engine.Decks.Deck.CardAddedDelegate(Deck_CardAdded);
			this.Player.Deck.CardRemoved += new Wormhole.Engine.Decks.Deck.CardRemovedDelegate(Deck_CardRemoved);

			// subscribe to mission events
			this.Player.Missions.CardAdded += new Wormhole.Engine.Decks.Deck.CardAddedDelegate(Missions_CardAdded);
			this.Player.Missions.CardRemoved += new Wormhole.Engine.Decks.Deck.CardRemovedDelegate(Missions_CardRemoved);
		}
		#endregion

		#region Display Player Information
		/// <summary>
		/// Display the player information.
		/// </summary>
		private void DisplayPlayerInformation()
		{
			if (this.Player != null)
			{
				this.lblDeck.Content = this.Player.Deck.Count;
				this.lblMissions.Content = this.Player.Missions.Count;
			}
		}
		#endregion

		#region Event Handlers

		#region Card Taken
		/// <summary>
		/// Card Taken
		/// </summary>
		private void Deck_CardTaken(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPlayerInformation();
			});
		}
		#endregion

		#region Mouse Down
		/// <summary>
		/// Mouse Down
		/// </summary>
		private void areaDeck_MouseDown(object sender, MouseButtonEventArgs e)
		{
			if (e.LeftButton == MouseButtonState.Pressed)
				this.TakeCard();
		}
		#endregion

		#region Mission Mouse Down
		/// <summary>
		/// The event handler to play a mission.
		/// </summary>
		private void areaMission_MouseDown(object sender, MouseButtonEventArgs e)
		{
			this.PlayMission();
		}
		#endregion

		#region Card Added
		/// <summary>
		/// The card added event.
		/// </summary>
		/// <param name="card">The card that was added</param>
		private void Deck_CardAdded(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPlayerInformation();
			});
		}
		#endregion

		#region Card Removed
		/// <summary>
		/// Handle the card removed event
		/// </summary>
		/// <param name="card">The card that was removed.</param>
		private void Deck_CardRemoved(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPlayerInformation();
			});
		}
		#endregion

		#region Mission Added
		/// <summary>
		/// Handle the event when a card is added to the missions pile.
		/// </summary>
		/// <param name="card">The mission card that was added.</param>
		private void Missions_CardAdded(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPlayerInformation();
			});
		}
		#endregion

		#region Mission Removed
		/// <summary>
		/// Handle the event when a card is removed from the missions pile.
		/// </summary>
		/// <param name="card">The card that was removed.</param>
		private void Missions_CardRemoved(Wormhole.Engine.CardLibrary.BaseCardTypes.Card card)
		{
			this.Dispatcher.Invoke(
				DispatcherPriority.Normal,
				(System.Windows.Forms.MethodInvoker)delegate()
			{
				this.DisplayPlayerInformation();
			});
		}
		#endregion

		#endregion

		#region Take Card
		/// <summary>
		/// Take a card that should be placed in the player's hand.
		/// </summary>
		private void TakeCard()
		{
			if (this.Player != null)
				this.Player.Deck.TakeCard();
		}
		#endregion

		#region Play Mission
		/// <summary>
		/// Play a mission.
		/// </summary>
		protected void PlayMission()
		{
			if (this.Player != null)
				this.Player.Missions.PlayMission();
		}
		#endregion
	}
}
